#!/usr/bin/env python
# -*- coding: utf-8 -*-
# File  :user_role_permission.py
# Time  :2025/4/22 13:53
# Email :fortunatedong@163.com
# Author:by Adonli
import json
import os

import toml

from config import ROOT_DIR

USERS_ROLE_PERMISSION_FILE = os.path.join(ROOT_DIR, 'settings/users_role_permission.json')
USERS_ROLE_PERMISSION_RELATION_FILE = os.path.join(ROOT_DIR, 'settings/users_role_permission_relation.json')
configs_toml = os.path.join(ROOT_DIR, 'settings/configs.toml')
with open(configs_toml, "r", encoding="utf-8") as f:
    cfg = toml.load(f)
permission_options_list = cfg['PAGES']['PAGES_NAVIGATION_LIST']
# 加载用户数据
def load_users():
    try:
        with open(USERS_ROLE_PERMISSION_FILE, 'r', encoding='utf-8') as f:
            return json.load(f)
    except (FileNotFoundError, json.JSONDecodeError):
        return init_users()


# 保存用户数据
def save_users(default_users_role_permission):
    os.makedirs(os.path.dirname(USERS_ROLE_PERMISSION_FILE), exist_ok=True)
    with open(USERS_ROLE_PERMISSION_FILE, 'w', encoding='utf-8') as f:
        json.dump(default_users_role_permission, f, ensure_ascii=False, indent=4)
def init_users():
    if not os.path.exists(USERS_ROLE_PERMISSION_FILE):
        default_users_role_permission = {
            "USERS":[
                {"userid": "admin", "password": "admin", "department": "研发部门", "rolecode": "admin","rolename":"管理员策略"},
                {"userid": "hejianying", "password": "hjy123456", "department": "电商部门", "rolecode": "e-commerce","rolename":"电商部策略"},
                {"userid": "huangkaiting", "password": "hkt123456", "department": "电商部门", "rolecode": "e-commerce","rolename":"电商部策略"},
                {"userid": "chenminqin", "password": "cmq123456", "department": "人事部门", "rolecode": "hr","rolename":"人事部策略"},
                {"userid": "chenyan", "password": "cy123456", "department": "人事部门", "rolecode": "hr","rolename":"人事部策略"},
                {"userid": "luoxiang", "password": "lx123456", "department": "人事部门", "rolecode": "hr","rolename":"人事部策略"},
            ],
            "ROLES":[
                {"rolecode": "admin", "rolename": "管理员策略"},
                {"rolecode": "e-commerce", "rolename": "电商部策略"},
                {"rolecode": "hr", "rolename": "人事部策略"},
            ],
            "PERMISSIONPAGE":[
                {"rolecode": "admin", "rolename": "管理员策略","permission":permission_options_list},
                {"rolecode": "e-commerce", "rolename": "电商部策略", "permission": ["账户管理","国铁商城比价系统","京东平台工具"]},
                {"rolecode": "hr", "rolename": "人事部策略", "permission": ["账户管理","人力资源工具"]},
            ]
        }
        save_users(default_users_role_permission)
    return load_users()




def authenticate_user(username, password):
    users_role_permission = load_users()
    """验证用户名和密码是否正确"""
    return next(
        (user for user in users_role_permission["USERS"]
         if user["userid"] == username and user["password"] == password),
        None
    )


def get_user_permissions(username, password):
    users_role_permission = load_users()
    authenticate = authenticate_user(username, password)
    rolecode = authenticate["rolecode"]

    permission_entry = next(
        (p for p in users_role_permission["PERMISSIONPAGE"]
         if p["rolecode"] == rolecode),
        None
    )
    return permission_entry